package com.kgc.crowd.distributioncrowdwebui.controller;

import com.kgc.crowd.distributioncrowdcommon.VO.MemberSignSuccessVO;
import com.kgc.crowd.distributioncrowdcommon.VO.MemberVO;
import com.kgc.crowd.distributioncrowdcommon.api.MemberMangerRemoteService;
import com.kgc.crowd.distributioncrowdcommon.pojo.ResultEntity;
import com.kgc.crowd.distributioncrowdcommon.util.CrowdConstant;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
@RequestMapping("/member/manager")
@Controller
public class MemberController {
    @Resource
    MemberMangerRemoteService memberMangerRemoteService;
    @RequestMapping("/do/login")
    public String login(MemberVO memberVO, HttpSession session, Model model){
        String loginacct = memberVO.getLoginacct();
        String userpswd = memberVO.getUserpswd();
        System.out.println(memberVO.toString());
        ResultEntity<MemberSignSuccessVO> loginResultEntity = memberMangerRemoteService.login(loginacct, userpswd);
        if(loginResultEntity.getResult().equals(ResultEntity.FAILED)){
            model.addAttribute(CrowdConstant.ATTR_NAME_MESSAGE,CrowdConstant.MESSAGE_ACCESS_DENIED);
            return "login";
        }
        MemberSignSuccessVO loginResultEntityData = loginResultEntity.getData();
        session.setAttribute(CrowdConstant.ATTR_NAME_LOGIN_MEMBER,loginResultEntityData);
        return "member";
    }
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        MemberSignSuccessVO memberSignSuccessVO = (MemberSignSuccessVO)session.getAttribute(CrowdConstant.ATTR_NAME_LOGIN_MEMBER);
        if(memberSignSuccessVO==null){
            return "redirect:/index";
        }
        String token = memberSignSuccessVO.getToken();
        ResultEntity<String> resultEntity = memberMangerRemoteService.logout(token);
        if(ResultEntity.FAILED.equals(resultEntity.getResult())){
                throw new RuntimeException(resultEntity.getMessage());
        }
        session.removeAttribute(CrowdConstant.ATTR_NAME_LOGIN_MEMBER);
        return "redirect:/index";
    }
    @RequestMapping("/send/code")
    @ResponseBody
    public ResultEntity<String> send(String phoneNum){
        ResultEntity<String> resultEntity = memberMangerRemoteService.sendCode(phoneNum);
        return resultEntity;
    }
    @RequestMapping("/register")
    public String register(MemberVO memberVO,Model model){
        ResultEntity<String> register=memberMangerRemoteService.register(memberVO);
        if (register.getResult().equals(ResultEntity.FAILED)){
            model.addAttribute(CrowdConstant.ATTR_NAME_MESSAGE,register.getMessage());
            return "reg";
        }
        return "login";
    }

    @RequestMapping("/minecrowdfunding")
    public String tominecrowdfunding(){
        return "minecrowdfunding";
    }
    @RequestMapping("/member")
    public String tomember(){
        return "member";
    }
}
